//: [Previous](@previous)

import UIKit
import PlaygroundSupport


extension UIView {
    func fadeTransition(duration:CFTimeInterval) {
        let animation:CATransition = CATransition()
        animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
        animation.type = kCATransitionFade
        animation.duration = duration
        self.layer.add(animation, forKey: kCATransitionFade)
    }
}

let view = UIView()
let width = 100
let height = 100

view.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
view.backgroundColor = #colorLiteral(red: 0.9529411793, green: 0.6862745285, blue: 0.1333333403, alpha: 1)



let rectQRCode = CGRect(
    origin: CGPoint(x: 25 , y: 25),
    size : CGSize( width: width/2  , height:  height/2  )
)


let overlay: CAShapeLayer = {
    let overlay = CAShapeLayer()
//    overlay.backgroundColor = UIColor.lightGray.cgColor
    overlay.fillColor       = UIColor.gray.withAlphaComponent(0.6).cgColor
    overlay.strokeColor     = UIColor.white.cgColor
    overlay.lineWidth       = 1
    overlay.fillRule = kCAFillRuleEvenOdd
    
    return overlay
}()




func buildBackgroundPath(size: CGSize, clearSize: CGSize) -> CGPath {
    let backframe = CGRect(origin: .zero, size: size)
    let clearX = (size.width  - clearSize.width )/2.0
    let clearY = (size.height - clearSize.height)/2.0
    let centerFrame = CGRect(origin: CGPoint(x: clearX, y: clearY),
                             size: clearSize)
    
    let path = UIBezierPath(rect: backframe)
    path.append(UIBezierPath(roundedRect: centerFrame, cornerRadius: 5))
    return path.cgPath
}




let path = buildBackgroundPath(size: view.frame.size, clearSize: CGSize.init(width: 30, height: 30))
//path.usesEvenOddFillRule = true
overlay.path = path

view.layer.addSublayer(overlay)

/*
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1)) {
    print("start change")
    view.backgroundColor = #colorLiteral(red: 0.4666666687, green: 0.7647058964, blue: 0.2666666806, alpha: 1)
//    view.fadeTransition(duration: 1.0)
    //PlaygroundPage.current.finishExecution()
}
 */




PlaygroundPage.current.liveView = view
PlaygroundPage.current.needsIndefiniteExecution = true



enum RouterAction {
    case push(Router)
    case present(Router)
}

extension UIViewController {
    
    func show(router: RouterAction) {
        switch router {
        case .push(let router):
            push(router: router)
        case .present(let router):
            present(router: router)
        }
    }
    
    func push(router: Router) {
        navigationController?.pushViewController(router.constructVC(), animated: true)
    }
    
    func present(router: Router) {
        present(router.constructVC(), animated: true, completion: nil)
    }
}

enum Router {
    case user(User)
    case activity(Activity)
    case sociaty(Sociaty)
}

extension Router {
    
    func constructVC() -> UIViewController {
        switch self {
        case .user(let router):
            print(router)
        case .activity(let router):
            print(router)
        case .sociaty(let router):
            print(router)
        }
        return UIViewController()
    }
}

extension Router {
    enum User {
        case login
        case me
        case profile
    }
}

extension Router.User {
    
    func construct() -> UIViewController {
        switch self {
        case .login:
            break
        case .me:
            break
        case .profile:
            break
        }
        return UIViewController()
    }
}




extension Router {
    enum Activity {
        case list
        case detail(String)
    }
}

extension Router.Activity {
    func construct() -> UIViewController {
        switch self {
        case .list:
            break
        case .detail(let detailId):
            print(detailId)
        }
        return UIViewController()
    }
}

extension Router {
    enum Sociaty {
        case list
        case detail(String)
    }
}


extension Router.Sociaty {
    func construct() -> UIViewController {
        switch self {
        case .list:
            break
        case .detail(let detailId):
            print(detailId)
        }
        
        return UIViewController()
    }
}
//: [Next](@next)
